<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin_accessory
 *
 * @author duyhoa
 */
class admin_accessory extends CI_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        set_validation_error_delimiter(); //Ckeditor's configuration
    }

    function index() {
        $data['subtitle'] = 'Danh sách tất cả linh kiện';
        $data['site'] = 'tp-admin/site/accessory/accessory-listview';

        $page = $this->uri->segment(4);
        if ($page <= 1)
            $page = 0;
        else
            $page = ($page - 1) * PAGE_SIZE;
        $product = $this->accessory_model->getAllAccessories_Admin($page);
        $data['list'] = $product['list'];
        $total = $product['total'];
        $link = ADMIN_PAGE_ACCESSORY;
        set_paging($link, $total, 4);
        $this->load->view('tp-admin/template', $data);
    }

    function add_accessory() {

        $data['categories'] = $this->category_model->getAllAccessoryCategories();
        $data['subtitle'] = 'Thêm linh kiện mới';

        $data['site'] = 'tp-admin/site/accessory/accessory-add';

        if ($this->input->post('submit') && $this->check_validation()) {

            $config['upload_path'] = './uploaded/';
            $config['allowed_types'] = 'jpg|jpeg|gif|png';

            $this->load->library('upload', $config);
            if (!$this->upload->do_upload()) {

                $this->session->set_userdata(array('error_msg' => $this->upload->display_errors()));
                redirect(current_url());
            }

            $config1['source_image'] = $this->upload->upload_path . $this->upload->file_name;
            $config1['maintain_ratio'] = FALSE;
            $config1['width'] = 300;
            $config1['height'] = 300;

            $this->load->library('image_lib', $config1);

            if (!$this->image_lib->resize()) {
                $this->session->set_flashdata('message', $this->image_lib->display_errors('<p class="error">', '</p>'));
            }


            $info = $this->post_input();
            $info['accessory_is_delete']=0;
            $info['accessory_id'] = generate_number_code(8);
            $info['accessory_name_segment'] = utf8_to_ascii($info['accessory_name'], '-');
            $info['accessory_tag'] = replace_space_to_dash($info['accessory_name'], ', ') . ', ' . replace_space_to_dash($info['accessory_name_segment'], ', ');
            $file_data = $this->upload->data();
            $info['accessory_image'] = $file_data['file_name'];
            if ($this->accessory_model->addAccessory($info)) {
                $this->session->set_userdata(array('success_msg' => 'Thêm linh kiện thành công'));
                redirect(current_url());
            } else {
                $this->session->set_userdata(array('error_msg' => 'Thêm linh kiện thất bại'));
                redirect(current_url());
            }
        }

        $this->load->view('tp-admin/template', $data);
    }

    function update_accessory() {

        $id=$this->uri->segment(4);
        $data['accessory_info']=$this->accessory_model->getAccessoryByID($id);
        
        $data['categories'] = $this->category_model->getAllAccessoryCategories();
        $data['subtitle'] = 'Cập nhật linh kiện';

        $data['site'] = 'tp-admin/site/accessory/accessory-update';

        if ($this->input->post('submit') && $this->check_validation()) {


            $info = $this->post_input();
            
            
            $info['accessory_name_segment'] = utf8_to_ascii($info['accessory_name'], '-');
            $info['accessory_tag'] = replace_space_to_dash($info['accessory_name'], ', ') . ', ' . replace_space_to_dash($info['accessory_name_segment'], ', ');
            if ($this->accessory_model->updateAccessory($info,$id)) {
                $this->session->set_userdata(array('success_msg' => 'Cập nhật linh kiện thành công'));
                redirect(current_url());
            } else {
                $this->session->set_userdata(array('error_msg' => 'Cập nhật linh kiện thất bại'));
                redirect(current_url());
            }
        }

        $this->load->view('tp-admin/template', $data);
    }

    function delete_accessory() {

        $id = $this->uri->segment(4);

        $data['site'] = 'tp-admin/site/accessory/accessory-listview';

        $this->accessory_model->deleteAccessory($id);

        redirect(HOME_PAGE.ADMIN_PAGE_ACCESSORY);
    }

    function post_input() {
        $info = array(
            'accessory_name' => $this->input->post('accessory_name'),
            'accessory_price' => $this->input->post('accessory_price'),
            'accessory_detail' => $this->input->post('accessory_detail'),
            'accessory_national' => $this->input->post('accessory_national'),
            'accessory_support' => $this->input->post('accessory_support'),
            'accessory_guarantee' => $this->input->post('accessory_guarantee'),
            'category_id' => $this->input->post('accessory_category')
        );
        return $info;
    }

    function check_validation() {
        $check_validation = array(
            array(
                'field' => 'accessory_name',
                'label' => 'Tên linh kiện',
                'rules' => 'trim|required'
            ), array(
                'field' => 'accessory_price',
                'label' => 'Giá linh kiện',
                'rules' => 'trim|required|integer|greater_than[0]'
            )
        );
        $this->form_validation->set_rules($check_validation);
        return $this->form_validation->run();
    }

}

?>
